#include "StdAfx.h"
#include "datatablemanagerbase.h"
#include "tableviewbase.h"

DataTableManagerBase::DataTableManagerBase( QObject * parent )
:   m_standardModel( parent ),
    m_sortFilterProxyModel( parent )
{
}

DataTableManagerBase::~DataTableManagerBase()
{
}

SortFilterTableModel & DataTableManagerBase::sortModel()
{
    return m_sortFilterProxyModel;
}

const SortFilterTableModel & DataTableManagerBase::sortModel() const
{
    return m_sortFilterProxyModel;
}

QStandardItemModel & DataTableManagerBase::tableModelBase()
{
    return m_standardModel;
}

const QStandardItemModel & DataTableManagerBase::tableModelBase() const
{
    return m_standardModel;
}

bool DataTableManagerBase::select( const QString & filterStr  )
{
    m_standardModel.clear();
    bool retValue = selectToTableModel( filterStr );
    if (retValue)
    {
        m_sortFilterProxyModel.setSourceModel( & m_standardModel );
    }

    return retValue;
}

bool DataTableManagerBase::select( const QString & filterStr, TableViewBase & tableViewBase )
{
    QVariant selectedData;
    QItemSelectionModel * selectionModel = tableViewBase.selectionModel();
    if (selectionModel != NULL)
    {
        QModelIndexList modelIndexList = selectionModel->selectedIndexes();
        if (! modelIndexList.isEmpty())
        {
            selectedData = m_sortFilterProxyModel.data( * modelIndexList.begin(), Qt::UserRole + 1 );
        }
    }

    return _select( filterStr, tableViewBase, selectedData );
}

bool DataTableManagerBase::_select( const QString & filterStr, TableViewBase & tableViewBase, QVariant & selectedData )
{
    m_standardModel.clear();
    bool retValue = selectToTableModel( filterStr );
    if (retValue)
    {
        m_sortFilterProxyModel.setSourceModel( & m_standardModel );

        if (selectedData.isValid())
        {
            int rowCount = m_sortFilterProxyModel.rowCount();
            for (int i = 0; i < rowCount; i++)
            {
                if (selectedData == m_sortFilterProxyModel.data( m_sortFilterProxyModel.index( i, 0 ), Qt::UserRole + 1 ))
                {
                    tableViewBase.selectRow( i );
                    return retValue;
                }
            }
        }   
    }

    return retValue;
}

void DataTableManagerBase::selectRow( int standardModelRow )
{
    QStandardItem * item = m_standardModel.item( standardModelRow, 0 );
    if (item != NULL)
    {
        int targetRow = m_sortFilterProxyModel.mapFromSource( m_standardModel.indexFromItem( item ) ).row();
        emit selectRowSignal( targetRow );
    }
}

void DataTableManagerBase::removeRow( int standardModelRow )
{
    m_standardModel.removeRows( standardModelRow, 1 );
}

bool DataTableManagerBase::_getStandardModelRowAndStandardItemFromProxyIndex( const QModelIndex & proxyModelIndex, int & standardModelRow, const QStandardItem *& item ) const
{
    if (proxyModelIndex.isValid())
    {
        standardModelRow = m_sortFilterProxyModel.mapToSource( proxyModelIndex ).row();
        item = m_standardModel.item( standardModelRow, 0 );
        return true;
    }


    standardModelRow = -1;
    item = NULL;
    return false;
}

bool DataTableManagerBase::getStandardModelRowAndStandardItemFromProxyIndex( const QModelIndex & proxyModelIndex, int & standardModelRow, const QStandardItem *& item ) const
{
    return _getStandardModelRowAndStandardItemFromProxyIndex( proxyModelIndex, standardModelRow, item );
}

bool DataTableManagerBase::getStandardModelRowAndStandardItemFromProxyIndex( const QItemSelectionModel * selectionModel, int & standardModelRow, const QStandardItem *& item ) const
{
    if (selectionModel != NULL)
    {
        QModelIndexList modelIndexList = selectionModel->selectedIndexes();
        if (! modelIndexList.isEmpty())
        {
            return _getStandardModelRowAndStandardItemFromProxyIndex( * modelIndexList.begin(), standardModelRow, item );
        }
    }

    standardModelRow = -1;
    item = NULL;
    return false;
}

//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

ManagerBase::ManagerBase( QWidget * parent )
:   m_parent( * parent )
{
}

ManagerBase::~ManagerBase()
{
}

ManagerBase::ActionStructVector ManagerBase::createActionList( const QStandardItem * item ) const
{
    ManagerBase::ActionStructVector actionStructVector;
    actionStructVector.push_back( ActionStruct( ActionBase::New, QObject::tr( "Add" ) ) );
    if (item == NULL)
        return actionStructVector;

    QVariant variant = item->data( Qt::UserRole + 1 );
    if (variant.isValid())
    {
        actionStructVector.push_back( ActionStruct( ActionBase::Update, QObject::tr( "Edit" ) ) );
        actionStructVector.push_back( ActionStruct( ActionBase::Delete, QObject::tr( "Delete" ) ) );
    }

    return actionStructVector;
}

ManagerBase::ActionStructVector ManagerBase::createActionList( const StandardItemVector & standardItemVector ) const
{
    return ManagerBase::ActionStructVector();
}

void ManagerBase::executeAction( int actionId, const StandardModelRowVector & standardModelRowVector, const StandardItemVector & standardItemVector )
{
    // ked uz potrebujes multi selekciu, musi to byt implementovane v odvodenej triede
    assert( 0 );
}
